package com.zoagroup.funzy.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;


/**
 * The persistent class for the postwall database table.
 * 
 */
@Entity
public class Postwall implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="id_wall", unique=true, nullable=false)
	private int idWall;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="creation_date")
	private Date creationDate;

	//bi-directional many-to-one association to Group
	@OneToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="id_group")
	private Group group;
	
	@ManyToMany(cascade=CascadeType.ALL)
	@JoinTable(name="postwall_message"
		, joinColumns={@JoinColumn(name="id_wall")}
		, inverseJoinColumns={@JoinColumn(name="id_msg")})
	private List<Message> messages;
	
	public Postwall() {
	}

	public Date getCreationDate() {
		return this.creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public Group getGroup() {
		return this.group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public List<Message> getMessages() {
		return messages;
	}

	public void setMessages(List<Message> messages) {
		this.messages = messages;
	}
	
	public Message addMessage(Message message) {
		if(messages == null) {
			messages = new LinkedList<Message>();
		}
		getMessages().add(message);
		return message;
	}

	public Message removeMessage(Message message) {
		if(messages == null) {
			messages = new LinkedList<Message>();
		}
		getMessages().remove(message);

		return message;
	}

	public int getIdWall() {
		return idWall;
	}

	public void setIdWall(int idWall) {
		this.idWall = idWall;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + idWall;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Postwall other = (Postwall) obj;
		if (idWall != other.idWall)
			return false;
		return true;
	}
}